# -*- coding: utf-8 -*-
# Author zwhset

from flask import Blueprint, request, render_template, flash, session, redirect

from modules.users import auth_user as _auth_user

b_auth_user = Blueprint("b_auth_user", __name__)

user = _auth_user.User()

@b_auth_user.route("/register", methods=["GET", "POST"])
def register():
    if request.method == "POST":
        username = request.values.get("username", None)
        password = request.values.get("password", None)
        password2 = request.values.get("password2", None)

        if not username or not password or not password2:
            flash("Params Fail.", "danger")
            return redirect("/users")

        if password != password2:
            flash("password Fail", "danger")
            return redirect("/users")

        if len(username) <= 5:
            flash("username width > 5", "danger")
            return redirect("/users")

        category = "danger"
        ok, message = user.add(username, password)
        if ok:
            category = "success"

        flash(message, category)

    return render_template("register.html")

@b_auth_user.route("/login", methods=["GET", "POST"])
def login():
    '''登陆逻辑'''
    if request.method == "POST":
        username = request.values.get("username", None)
        password = request.values.get("password", None)
        token = request.values.get("token", None)

        ok, message = user.check(username, password, token)
        if ok:
            session["username"] = username
            return redirect("/")
        else:
            category = "danger"

        flash(message, category)

    return render_template("login.html")

@b_auth_user.route("/logout")
def logout():
    '''登出'''

    del session["username"]
    return redirect("/login")

@b_auth_user.route("/users", methods=["GET", "POST"])
def users():
    if request.method == "POST":

        # user search
        key = request.values.get("key", "")
        if key:
            users = user.select(username=key)
            return render_template("auth_users.html", users=users,
                                   key=key)

        # useradd
        useradd = request.values.get("useradd", "")
        if useradd:
            username = request.values.get("username", None)
            password = request.values.get("password", None)
            password2 = request.values.get("password2", None)

            if not username or not password or not password2:
                flash("Params Fail.", "danger")
                return redirect("/users")

            if password != password2:
                flash("password Fail", "danger")
                return redirect("/users")

            if len(username) <= 5:
                flash("username width > 5", "danger")
                return redirect("/users")

            category = "danger"
            ok, message = user.add(username, password)
            if ok:
                category = "success"

            flash(message, category)

        # userdel
        userdel = request.values.get("userdel", "")
        if userdel:
            id = request.values.get("id", "", type=int)
            if not id:
                flash("Params Fail.", "danger")

            category = "danger"
            ok, message = user.delete(id)
            if ok:
                category = "success"

            flash(message, category)


    users = user.select()
    return render_template("auth_users.html", users=users)

